package ReefStats::Controller::REST;

use strict;
use warnings;
use base qw(Catalyst::Controller::REST);

__PACKAGE__->config->{'default'} = 'text/x-json';

sub users_by_domain : Local : ActionClass('REST') {
    my ( $self, $c, $id ) = @_;
    $c->stash( id => $id );
}

sub users_by_domain_GET {
    my ( $self, $c ) = @_;

    my $id = $c->stash->{id};

    my $domain = $c->model('ReefStatsDB::Domains')->find( { id => $id } );

    if ($domain) {
        $self->status_ok(
            $c,
            entity => [
                map { [ $_->id, $_->email_address ] }
                  $c->model('ReefStatsDB::Users')->search(
                    { domain => $domain->domain },
                    { join   => 'domain', order_by => 'email_address' }
                  )
            ]
        );
    }
    else {
        $self->status_not_found( $c, message => 'Unable to find domain' );
    }
}

1;

__END__

